//
//  RegisterView.m
//  ECO
//
//  Created by 谷银行 on 2019/9/23.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "RegisterView.h"

@implementation RegisterView
- (UILabel *)RegTitle{
    if (!_RegTitle) {
        _RegTitle = [[UILabel alloc]init];
        _RegTitle.text = @"登录";
        _RegTitle.font = [UIFont systemFontOfSize:18 weight:1.5];
    }
    return _RegTitle;
}
- (UIButton *)exitBtn{
    if (!_exitBtn ) {
        _exitBtn = [[UIButton alloc]init];
        [_exitBtn setImage:[UIImage imageNamed:@"nav_icon_back"] forState:UIControlStateNormal];
        [_exitBtn addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exitBtn;
}
- (UILabel *)Title{
    if (!_Title) {
        _Title = [[UILabel alloc]init];
        _Title.text = @"手机号登录";
        _Title.font = [UIFont systemFontOfSize:14];
    }
    return _Title;
}
- (UILabel *)Phone{
    if (!_Phone) {
        _Phone = [[UILabel alloc]init];
        _Phone.text = @"账号";
        _Phone.textColor = [UIColor colorWithHexString:@"#999999"];
        _Phone.font = [UIFont systemFontOfSize:13];
    }
    return _Phone;
}
- (UITextField *)PhoneText{
    if (!_PhoneText) {
        _PhoneText = [[UITextField alloc]init];
//        _PhoneText.keyboardType = UIKeyboardTypeNumberPad;
        _PhoneText.placeholder = @"请输入手机号";
        _PhoneText.clearsOnBeginEditing = NO;
        _PhoneText.clearButtonMode = UITextFieldViewModeWhileEditing;
        _PhoneText.delegate = self;
    }
    return _PhoneText;
}
- (UILabel *)Password{
    if (!_Password) {
        _Password = [[UILabel alloc]init];
        _Password.text = @"密码";
        _Password.textColor = [UIColor colorWithHexString:@"#999999"];
        _Password.font = [UIFont systemFontOfSize:13];
    }
    return _Password;
}
- (UITextField *)PasswordText{
    if (!_PasswordText) {
        _PasswordText = [[UITextField alloc]init];
        _PasswordText.delegate = self;
//        _PasswordText.keyboardType = UIKeyboardTypeNumberPad;
        _PasswordText.placeholder = @"请输入密码";
        [_PasswordText setSecureTextEntry:YES];
    }
    return _PasswordText;
}
- (UIView *)phoneLine{
    if (!_phoneLine) {
        _phoneLine = [[UIView alloc]init];
        _phoneLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    }
    return _phoneLine;
}
- (UIView *)PasswordLine{
    if (!_PasswordLine) {
        _PasswordLine = [[UIView alloc]init];
        _PasswordLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    }
    return _PasswordLine;
}
- (UIButton *)RegistBtn{
    if (!_RegistBtn) {
        _RegistBtn = [[UIButton alloc]init];
        [_RegistBtn setTitle:@"登录" forState:UIControlStateNormal];
        _RegistBtn.backgroundColor = [UIColor colorWithHexString:@"#00DAAF"];
        _RegistBtn.layer.cornerRadius = 25;
        _RegistBtn.layer.masksToBounds = YES;
        
        [_RegistBtn addTarget:self action:@selector(RegisterButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _RegistBtn;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self addSubview:self.RegTitle];
        [self addSubview:self.exitBtn];
        [self addSubview:self.Title];
        [self addSubview:self.Phone];
        [self addSubview:self.PhoneText];
        [self addSubview:self.phoneLine];
        [self addSubview:self.Password];
        [self addSubview:self.PasswordText];
        [self addSubview:self.PasswordLine];
        [self addSubview:self.RegistBtn];
        [self RView];
    }
    return self;
}
- (void)RView{
    
    // 登录
    [_RegTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(45);
        make.left.equalTo(self).offset(15);
        make.width.equalTo(@(50));
        make.height.equalTo(@(25));
    }];
    // 退出
    [_exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(45);
        make.right.equalTo(self).offset(-12);
        make.width.equalTo(@(25));
        make.height.equalTo(@(25));
    }];
    // 标题
    [_Title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.RegTitle.mas_bottom).offset(84);
        make.left.equalTo(self).offset(22);
        make.width.equalTo(@(100));
        make.height.equalTo(@(20));
    }];
    // phonetitle
    [_Phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.Title.mas_bottom).offset(26);
        make.left.equalTo(self).offset(22);
        make.width.equalTo(@(40));
        make.height.equalTo(@(18));
    }];
    // Phonetextfiled
    [_PhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.Phone.mas_bottom).offset(8);
        make.left.equalTo(self).offset(22);
        make.centerX.equalTo(self);
        make.height.equalTo(@(20));
    }];
    // line
    [_phoneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PhoneText.mas_bottom).offset(8);
        make.left.equalTo(self).offset(22);
        make.centerX.equalTo(self);
        make.height.equalTo(@(1));
    }];
    // Passwordtitle
    [_Password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneLine.mas_bottom).offset(30);
        make.left.equalTo(self).offset(22);
        make.width.equalTo(@(40));
        make.height.equalTo(@(18));
    }];
    // Passwordtextfiled
    [_PasswordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.Password.mas_bottom).offset(8);
        make.left.equalTo(self).offset(22);
        make.right.equalTo(self).offset(-22);
        make.height.equalTo(@(20));
    }];
    //  line
    [_PasswordLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PasswordText.mas_bottom).offset(9);
        make.left.equalTo(self).offset(22);
        make.centerX.equalTo(self);
        make.height.equalTo(@(1));
    }];
    //登录按钮
    [_RegistBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.PasswordLine.mas_bottom).offset(51);
        make.left.equalTo(self).offset(57);
        make.centerX.equalTo(self);
        make.height.equalTo(@(50));
    }];
    [[UITextField appearance] setTintColor:[UIColor colorWithHexString:@"#00DAAF"]];
    //通知
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    //注册通知
    [center addObserver:self selector:@selector(textValueChanged:) name:UITextFieldTextDidBeginEditingNotification object:self.PhoneText];
    [center addObserver:self selector:@selector(PasswordtextValueChanged:) name:UITextFieldTextDidBeginEditingNotification object:self.PasswordText];
}
- (void)exit{
    NSLog(@"sdfasfasfasfasdf");
    exit(0); 
}
// 登录按钮
- (void)RegisterButton{
    [self endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
    _PasswordLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    _phoneLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    if (self.delegate && [self.delegate respondsToSelector:@selector(RegisterViewClick:pass:)]) {
        [self.delegate performSelector:@selector(RegisterViewClick:pass:) withObject:_PhoneText.text withObject:_PasswordText.text];
    }
}
//  触摸视图收回键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES]; //实现该方法是需要注意view需要是继承UIControl而来的
    _PasswordLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    _phoneLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
}
///监听文本框的值的改变
- (void)textValueChanged:(NSNotification *)notice
{
    if(self.PhoneText.enabled == YES ){
        _phoneLine.backgroundColor = [UIColor colorWithHexString:@"#00DAAF"];
        _PasswordLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    }
}
- (void)PasswordtextValueChanged:(NSNotification *)notice
{
    if (self.PasswordText.enabled == YES){
        _PasswordLine.backgroundColor = [UIColor colorWithHexString:@"#00DAAF"];
        _phoneLine.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    }
    
}
@end
